package c.j.a.d.b.c;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.IInterface;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import android.util.SparseArray;
import c.j.a.d.b.e.t;
import c.j.a.d.b.j.h;
import c.j.a.d.b.j.i;
import com.ss.android.socialbase.downloader.downloader.SqlDownloadCacheService;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/* compiled from: SqlDownloadCacheAidlWrapper */
public class g implements ServiceConnection, t {
    public static boolean h;
    public static int i;
    public static long j;
    public c a;
    public Handler b = new Handler(Looper.getMainLooper());
    public b c = null;
    public d d;
    public Runnable e = new a();
    public Future<?> f;
    public CountDownLatch g = new CountDownLatch(1);

    /* compiled from: SqlDownloadCacheAidlWrapper */
    public class a implements Runnable {
        public void run() {
            if (!g.h) {
                d dVar = g.this.d;
                if (dVar != null) {
                    h.this.b = new e();
                    Log.e("DefaultDownloadCache", "rebind error,use backup sqlDownloadCache");
                }
            }
        }
    }

    /* compiled from: SqlDownloadCacheAidlWrapper */
    public class b implements Runnable {
        public final /* synthetic */ IBinder a;

        /* compiled from: SqlDownloadCacheAidlWrapper */
        public class a implements DeathRecipient {
            public void binderDied() {
                boolean z = false;
                g.h = false;
                g gVar = g.this;
                Objects.requireNonNull(gVar);
                if (VERSION.SDK_INT < 26 && !g.h) {
                    String str = "SqlDownloadCacheAidlWra";
                    if (g.i > 5) {
                        c.j.a.d.b.g.a.f(str, "bindMainProcess: bind too many times!!! ");
                    } else {
                        long currentTimeMillis = System.currentTimeMillis();
                        if (currentTimeMillis - g.j < 15000) {
                            c.j.a.d.b.g.a.f(str, "bindMainProcess: time too short since last bind!!! ");
                        } else {
                            g.i++;
                            g.j = currentTimeMillis;
                            gVar.b.postDelayed(new h(gVar), 1000);
                            z = true;
                        }
                    }
                }
                if (!z) {
                    g gVar2 = g.this;
                    if (gVar2.d != null) {
                        gVar2.b.postDelayed(gVar2.e, 2000);
                    }
                }
            }
        }

        public b(IBinder iBinder) {
            this.a = iBinder;
        }

        /* JADX WARNING: Missing exception handler attribute for start block: B:33:0x006e */
        /* JADX WARNING: Missing exception handler attribute for start block: B:26:0x005a */
        public void run() {
            /*
            r6 = this;
            monitor-enter(r6);
            r0 = 0;
            r1 = c.j.a.d.b.c.g.this;	 Catch:{ all -> 0x0020 }
            r2 = r1.c;	 Catch:{ all -> 0x0020 }
            if (r2 == 0) goto L_0x000f;
        L_0x0008:
            r1 = r1.a;	 Catch:{ all -> 0x0020 }
            if (r1 == 0) goto L_0x000f;
        L_0x000c:
            r1.e0(r2);	 Catch:{ all -> 0x0020 }
        L_0x000f:
            r1 = c.j.a.d.b.c.g.this;	 Catch:{ all -> 0x001e }
            r1 = r1.g;	 Catch:{ all -> 0x001e }
            r1.countDown();	 Catch:{ all -> 0x001e }
            r1 = r6.a;	 Catch:{ all -> 0x005a }
            r2 = new c.j.a.d.b.c.g$b$a;	 Catch:{ all -> 0x005a }
            r2.m2394init();	 Catch:{ all -> 0x005a }
            goto L_0x0057;
        L_0x001e:
            r0 = move-exception;
            goto L_0x006f;
        L_0x0020:
            r1 = move-exception;
            r2 = "SqlDownloadCacheAidlWra";
            r3 = "onServiceConnected fail";
            r4 = c.j.a.d.b.g.a.a;	 Catch:{ all -> 0x005c }
            r5 = 6;
            if (r4 > r5) goto L_0x0031;
        L_0x002a:
            r2 = c.j.a.d.b.g.a.c(r2);	 Catch:{ all -> 0x005c }
            android.util.Log.e(r2, r3, r1);	 Catch:{ all -> 0x005c }
        L_0x0031:
            r1 = c.j.a.d.b.c.g.this;	 Catch:{ all -> 0x005c }
            r1 = r1.d;	 Catch:{ all -> 0x005c }
            if (r1 == 0) goto L_0x0049;
        L_0x0037:
            r1 = (c.j.a.d.b.j.h.b) r1;	 Catch:{ all -> 0x005c }
            r1 = c.j.a.d.b.j.h.this;	 Catch:{ all -> 0x005c }
            r2 = new c.j.a.d.b.c.e;	 Catch:{ all -> 0x005c }
            r2.m8068init();	 Catch:{ all -> 0x005c }
            r1.b = r2;	 Catch:{ all -> 0x005c }
            r1 = "DefaultDownloadCache";
            r2 = "rebind error,use backup sqlDownloadCache";
            android.util.Log.e(r1, r2);	 Catch:{ all -> 0x005c }
        L_0x0049:
            r1 = c.j.a.d.b.c.g.this;	 Catch:{ all -> 0x001e }
            r1 = r1.g;	 Catch:{ all -> 0x001e }
            r1.countDown();	 Catch:{ all -> 0x001e }
            r1 = r6.a;	 Catch:{ all -> 0x005a }
            r2 = new c.j.a.d.b.c.g$b$a;	 Catch:{ all -> 0x005a }
            r2.m2394init();	 Catch:{ all -> 0x005a }
        L_0x0057:
            r1.linkToDeath(r2, r0);	 Catch:{ all -> 0x005a }
        L_0x005a:
            monitor-exit(r6);	 Catch:{ all -> 0x001e }
            return;
        L_0x005c:
            r1 = move-exception;
            r2 = c.j.a.d.b.c.g.this;	 Catch:{ all -> 0x001e }
            r2 = r2.g;	 Catch:{ all -> 0x001e }
            r2.countDown();	 Catch:{ all -> 0x001e }
            r2 = r6.a;	 Catch:{ all -> 0x006e }
            r3 = new c.j.a.d.b.c.g$b$a;	 Catch:{ all -> 0x006e }
            r3.m2394init();	 Catch:{ all -> 0x006e }
            r2.linkToDeath(r3, r0);	 Catch:{ all -> 0x006e }
        L_0x006e:
            throw r1;	 Catch:{ all -> 0x001e }
        L_0x006f:
            monitor-exit(r6);	 Catch:{ all -> 0x001e }
            throw r0;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.j.a.d.b.c.g$b.run():void");
        }
    }

    /* compiled from: SqlDownloadCacheAidlWrapper */
    public class c implements Runnable {
        public final /* synthetic */ SparseArray a;
        public final /* synthetic */ SparseArray b;
        public final /* synthetic */ d c;

        /* compiled from: SqlDownloadCacheAidlWrapper */
        public class a extends c.j.a.d.b.c.b.a {
            public void z0(Map map, Map map2) {
                c.j.a.d.b.n.a.l(c.this.a, map);
                c.j.a.d.b.n.a.l(c.this.b, map2);
                ((i) c.this.c).a();
                g.this.c(null);
            }
        }

        public c(SparseArray sparseArray, SparseArray sparseArray2, d dVar) {
            this.a = sparseArray;
            this.b = sparseArray2;
            this.c = dVar;
        }

        public void run() {
            int await;
            g.this.c(new a());
            try {
                await = g.this.g.await(5000, TimeUnit.MILLISECONDS) ^ 1;
            } catch (Throwable th) {
                th.printStackTrace();
                await = 0;
            }
            if (await != 0) {
                Future future = g.this.f;
                if (future != null) {
                    future.cancel(true);
                }
            }
            g gVar = g.this;
            Objects.requireNonNull(gVar);
            try {
                c cVar = gVar.a;
                if (cVar != null) {
                    cVar.a();
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            if (await != 0) {
                d dVar = this.c;
                if (dVar != null) {
                    ((i) dVar).a();
                }
            }
        }
    }

    /* compiled from: SqlDownloadCacheAidlWrapper */
    public interface d {
    }

    public g() {
        SqlDownloadCacheService.a(c.j.a.d.b.e.b.a(), this);
    }

    public com.ss.android.socialbase.downloader.g.c C(int i, long j) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.C(i, j);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void E(com.ss.android.socialbase.downloader.g.b bVar) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.E(bVar);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public com.ss.android.socialbase.downloader.g.c I(int i, long j, String str, String str2) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.I(i, j, str, str2);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public com.ss.android.socialbase.downloader.g.c O(int i, long j) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.O(i, j);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<com.ss.android.socialbase.downloader.g.c> a(String str) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.a(str);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public com.ss.android.socialbase.downloader.g.c b(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.b(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void c(b bVar) {
        synchronized (this) {
            c cVar = this.a;
            if (cVar != null) {
                try {
                    cVar.e0(bVar);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            } else {
                this.c = bVar;
            }
        }
    }

    public void d(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.d(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public com.ss.android.socialbase.downloader.g.c e(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.e(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void f(com.ss.android.socialbase.downloader.g.b bVar) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.f(bVar);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public com.ss.android.socialbase.downloader.g.c g(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.g(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public com.ss.android.socialbase.downloader.g.c h(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.h(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void i(int i, List<com.ss.android.socialbase.downloader.g.b> list) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.i(i, list);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public List<com.ss.android.socialbase.downloader.g.c> j(String str) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.j(str);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void k(int i, int i2, long j) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.k(i, i2, j);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public boolean l(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.l(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void m(int i, int i2, int i3, long j) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.m(i, i2, i3, j);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void n(int i, int i2, int i3, int i4) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.n(i, i2, i3, i4);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public com.ss.android.socialbase.downloader.g.c o(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.o(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        h = true;
        this.b.removeCallbacks(this.e);
        try {
            c cVar;
            int i = c.j.a.d.b.c.c.a.a;
            if (iBinder == null) {
                cVar = null;
            } else {
                IInterface queryLocalInterface = iBinder.queryLocalInterface("com.ss.android.socialbase.downloader.db.ISqlDownloadCacheAidl");
                if (queryLocalInterface == null || !(queryLocalInterface instanceof c)) {
                    cVar = new c.j.a.d.b.c.c.a.a(iBinder);
                } else {
                    cVar = (c) queryLocalInterface;
                }
            }
            this.a = cVar;
        } catch (Throwable th) {
            th.printStackTrace();
        }
        this.f = c.j.a.d.b.e.b.q().submit(new b(iBinder));
    }

    public void onServiceDisconnected(ComponentName componentName) {
        this.a = null;
        h = false;
    }

    public com.ss.android.socialbase.downloader.g.c t(int i, long j) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.t(i, j);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void y0(SparseArray<com.ss.android.socialbase.downloader.g.c> sparseArray, SparseArray<List<com.ss.android.socialbase.downloader.g.b>> sparseArray2, d dVar) {
        c.j.a.d.b.e.b.q().submit(new c(sparseArray, sparseArray2, dVar));
    }

    public com.ss.android.socialbase.downloader.g.c a(int i, int i2) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.a(i, i2);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<com.ss.android.socialbase.downloader.g.c> b(String str) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.b(str);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean d() {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.d();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean f(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.f(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean a(com.ss.android.socialbase.downloader.g.c cVar) {
        try {
            c cVar2 = this.a;
            if (cVar2 != null) {
                return cVar2.a(cVar);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void b() {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.b();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public List<com.ss.android.socialbase.downloader.g.c> c(String str) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.c(str);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void d(int i, List<com.ss.android.socialbase.downloader.g.b> list) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                cVar.d(i, list);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public com.ss.android.socialbase.downloader.g.c a(int i, long j) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.a(i, j);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void b(com.ss.android.socialbase.downloader.g.c cVar) {
        try {
            c cVar2 = this.a;
            if (cVar2 != null) {
                cVar2.b(cVar);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public List<com.ss.android.socialbase.downloader.g.b> c(int i) {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.c(i);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean c() {
        try {
            c cVar = this.a;
            if (cVar != null) {
                return cVar.c();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }
}
